package common;

import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import model.OrderSearch;

public class HttpClientOrder {
	public static String httpURLConnectionPOST(String urlString) {
		StringBuilder sb = new StringBuilder();
		try {
			URL url = new URL(urlString);

			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();

			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("GET");
			connection.setUseCaches(false);

			connection.setInstanceFollowRedirects(true);

			connection.setRequestProperty("accept", "application/json");
			connection.setRequestProperty("content-type", "application/json");
			connection.setRequestProperty("x-ml-appid",
					"5a71a3ea6b1f0a00074b97a8");
			connection.setRequestProperty("x-ml-masterkey",
					"RTRFUVNaUllGbTJ6c05BU3BHa09IUQ");
			connection.setRequestProperty("cache-control", "no-cache");

			connection.connect();

			BufferedReader bf = new BufferedReader(new InputStreamReader(
					connection.getInputStream(), "UTF-8"));
			String line;
			while ((line = bf.readLine()) != null) {
				sb.append(line).append(System.getProperty("line.separator"));
			}
			bf.close();
			connection.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("order" + sb.toString());
		return sb.toString();
	}

	public static void main(String[] args) {
		OrderSearch orderSearch = new OrderSearch();
		orderSearch.setMallId("5aaf23cfad16f000085fab3c");
		orderSearch.setStatus(2);
		Gson gson = new Gson();

		String whereJson = gson.toJson(orderSearch);
		try {
			whereJson = URLEncoder.encode(whereJson, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		String url = "https://wonapi.maxleap.cn/1.0/mall/reserves/order?skip=0&limit=10&order=-createdAt&where="
				+ whereJson;
		System.out.println(url);
		System.out.println(httpURLConnectionPOST(url));
	}
}